﻿using Common;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using SQLDLL;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Scale
{
    public partial class AddHierarchical : Form
    {
        #region 成员变量
        NursingForm formService = new NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        PatientInfo PatientInfo;
        int Sum = 0;
        string Id;
        Tb_User LoginUser;
        #endregion
        #region 默认构造函数2个 内部和外部调用
        public AddHierarchical(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.PatientInfo = patientInfo;
            this.LoginUser = loginUser;
            this.Id = id;

            //绑定数据
            BindInfo(id);
            ElectronicSignature();
        }
        public AddHierarchical(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;

            //绑定数据
            BindInfo(this.Id);
            ElectronicSignature();
        }
        public AddHierarchical(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;

            //绑定数据
            BindInfo(this.Id);
            ElectronicSignature();
        }
        #endregion
        /// <summary>
        /// 初始化加载印章图片
        /// </summary>
        private void ElectronicSignature()
        {
            ElectronicSignature es = new ElectronicSignature(LoginUser);
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textzerenhushiqianming, picElectronicSignature, textzerenhushiqianming.Location);
        }

        //保存
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text.Trim()))
                dateriqi.DateTime = DateTime.Now;

            NursingGrade xmnr = new NursingGrade();
            #region 提取页面数据到xml对象
            //进食
            xmnr.JinShi = radioGroupjinshi.EditValue == null ? "" : radioGroupjinshi.EditValue.ToString();
            //洗澡
            xmnr.XiZao = radioGroupxizao.EditValue == null ? "" : radioGroupxizao.EditValue.ToString();
            //修饰
            xmnr.XiuShi = radioGroupxiushi.EditValue == null ? "" : radioGroupxiushi.EditValue.ToString();
            //穿衣
            xmnr.ChuangYi = radioGroupchuanyi.EditValue == null ? "" : radioGroupchuanyi.EditValue.ToString();
            //控制大便
            xmnr.KongZhiDaBian = radioGroupkongzhidabian.EditValue == null ? "" : radioGroupkongzhidabian.EditValue.ToString();
            //控制小便
            xmnr.KongZhiXiaoBian = radioGroupkongzhixiaobian.EditValue == null ? "" : radioGroupkongzhixiaobian.EditValue.ToString();
            //如厕
            xmnr.RuCe = radioGroupruce.EditValue == null ? "" : radioGroupruce.EditValue.ToString();
            //床椅转移
            xmnr.ChuangYiZhuangYi = radioGroupchuangyizhuanyi.EditValue == null ? "" : radioGroupchuangyizhuanyi.EditValue.ToString();
            //平地行走
            xmnr.PingDiXingZou = radioGrouppingdixingzou.EditValue == null ? "" : radioGrouppingdixingzou.EditValue.ToString();
            //上下楼梯
            xmnr.ShangXiaLouTi = radioGroupshangxialouti.EditValue == null ? "" : radioGroupshangxialouti.EditValue.ToString();
            //自理能力
            xmnr.ZiLiNengLi = radioGroupzilinenglidengji.EditValue == null ? "" : radioGroupzilinenglidengji.EditValue.ToString();
            //病情等级    
            xmnr.BingQingDengJi = radioGroupbingqingdengji.EditValue == null ? "" : radioGroupbingqingdengji.EditValue.ToString();
            //护理级别
            xmnr.HuLiJiBie = combohulijibie.Text;
            string zerenhushiqianming = textzerenhushiqianming.Text;
            if (Id == "" || string.IsNullOrEmpty(zerenhushiqianming))//只记录首次录入护士签名
            {
                //责任护士签名
                xmnr.ZenRenHuShiQianMing = LoginUser.UserName;
            }
            else
            {
                xmnr.ZenRenHuShiQianMing = zerenhushiqianming;
            }
            //日期
            xmnr.RiQi = dateriqi.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //总分
            xmnr.ZongFen = textzongfen.Text;
            //表单ID
            xmnr.FormId = (int)FormType.PatientCare;
            //表单名字
            xmnr.FormName = EnumHelper.GetDescription(FormType.PatientCare);
            #endregion
            //表单ID
            record.FormId = (int)FormType.PatientCare;
            //表单名字
            record.FormName = EnumHelper.GetDescription(FormType.PatientCare);
            //住院号
            record.PatientId = PatientInfo.PATIENT_ID;
            //住院次数
            record.VisitId = PatientInfo.VISIT_ID;
            //患者姓名
            record.PatientName = PatientInfo.PATIENT_NAME;
            //床号
            record.BedNo = PatientInfo.BED_NO;
            //当前患者科室ID
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;
            //当前患者科室名称
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;
            //当前患者病区ID
            record.AreaId = PatientInfo.NURSE_CELL_CODE;
            //当前患者病区名称
            record.AreaName = PatientInfo.NURSE_CELL_NAME;
            record.NursingFormData = XmlUtil.ObjectToXML(xmnr, Encoding.UTF8);
            //时间
            record.AssessmentDate = dateriqi.DateTime;
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            var ret2 = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret2.S == true)
            {
                if (Base.ESSwitch.Equals("on"))//电签开始
                {
                    string signXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><riqi Name=\"日期\">" + dateriqi.Text + "</riqi><zongfewn Name=\"总分\">" + textzongfen.Text + "</zongfewn><jinshi Name=\"进食\">" + radioGroupjinshi.Text + "</jinshi>"
                    + "<xizao Name=\"洗澡\">" + radioGroupxizao.Text + "</xizao><xiushi Name=\"修饰\">" + radioGroupxiushi.Text + "</xiushi><chuanyi Name=\"穿衣\">" + radioGroupchuanyi.Text + "</chuanyi>"
                    + "<kongzhidabian Name=\"控制大便\">" + radioGroupkongzhidabian.Text + "</kongzhidabian><kongzhixiaobain Name=\"控制小便\">" + radioGroupkongzhixiaobian.Text + "</kongzhixiaobain>"
                    + "<ruce Name=\"如厕\">" + radioGroupruce.Text + "</ruce><chuangyizhuanyoi Name=\"床椅转移\">" + radioGroupchuangyizhuanyi.Text + "</chuangyizhuanyoi><pingdixingzou Name=\"平底行走\">" + radioGrouppingdixingzou.Text + "</pingdixingzou>"
                    + "<shangxialouti Name=\"上下楼梯\">" + radioGroupshangxialouti.Text + "</shangxialouti><zilinenglidengji Name=\"自理能力等级\">" + radioGroupzilinenglidengji.Text + "</zilinenglidengji><bingqingdnegji Name=\"病情等级\">" + radioGroupbingqingdengji.Text + "</bingqingdnegji>"
                    + "<hulijibie Name=\"护士级别\">" + combohulijibie.Text + "</hulijibie><hushiqianzi Name=\"护士签字\">" + textzerenhushiqianming.Text + "</hushiqianzi>";
                    //TODO:电签接口提交
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, EnumHelper.GetDescription(FormType.PatientCare) + (record.Id > 0 ? "-更新" : "-新增"), EnumHelper.GetDescription(FormType.PatientCare), record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool flag = es.GetElectronicSignatureImage(es, signXml, extraInfo, "14001", "护理分级表", PatientInfo.PATIENT_NAME, PatientInfo.VISIT_ID);
                    if (!flag) { MessageBox.Show("电子签名失败"); return; }
                }//电签结束
                MessageBox.Show(ret2.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret2.M);
            }
        }

        //显示--双击后显示对应的记录
        public void BindInfo(string Id)
        {
            textzerenhushiqianming.Text = this.LoginUser.UserName;
            textzongfen.Text = "";
            dateriqi.EditValue = DateTime.Now; // 默认显示当前日期 
            if (string.IsNullOrEmpty(Id)) return;
            string sql = @" SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}' ";
            sql = string.Format(sql, Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            NursingGrade xmnr = new NursingGrade();
            xmnr = XmlUtil.XMLToObject<NursingGrade>(NursingFormData, Encoding.UTF8);
            //进食
            radioGroupjinshi.SelectedIndex = Convert.ToInt32(xmnr.JinShi == "" ? "-1" : xmnr.JinShi);
            //洗澡
            radioGroupxizao.SelectedIndex = Convert.ToInt32(xmnr.XiZao == "" ? "-1" : xmnr.XiZao);
            //修饰
            radioGroupxiushi.SelectedIndex = Convert.ToInt32(xmnr.XiuShi == "" ? "-1" : xmnr.XiuShi);
            //穿衣
            radioGroupchuanyi.SelectedIndex = Convert.ToInt32(xmnr.ChuangYi == "" ? "-1" : xmnr.ChuangYi);
            //控制大便
            radioGroupkongzhidabian.SelectedIndex = Convert.ToInt32(xmnr.KongZhiDaBian == "" ? "-1" : xmnr.KongZhiDaBian);
            //控制小便
            radioGroupkongzhixiaobian.SelectedIndex = Convert.ToInt32(xmnr.KongZhiXiaoBian == "" ? "-1" : xmnr.KongZhiXiaoBian);
            //如厕
            radioGroupruce.SelectedIndex = Convert.ToInt32(xmnr.RuCe == "" ? "-1" : xmnr.RuCe);
            //床椅转移
            radioGroupchuangyizhuanyi.SelectedIndex = Convert.ToInt32(xmnr.ChuangYiZhuangYi == "" ? "-1" : xmnr.ChuangYiZhuangYi);
            //平地行走
            radioGrouppingdixingzou.SelectedIndex = Convert.ToInt32(xmnr.PingDiXingZou == "" ? "-1" : xmnr.PingDiXingZou);
            //上下楼梯
            radioGroupshangxialouti.SelectedIndex = Convert.ToInt32(xmnr.ShangXiaLouTi == "" ? "-1" : xmnr.ShangXiaLouTi);
            //自理能力
            radioGroupzilinenglidengji.SelectedIndex = Convert.ToInt32(xmnr.ZiLiNengLi == "" ? "-1" : xmnr.ZiLiNengLi);
            //病情等级
            radioGroupbingqingdengji.SelectedIndex = Convert.ToInt32(xmnr.BingQingDengJi == "" ? "-1" : xmnr.BingQingDengJi);
            //日期
            DateTime dt = Convert.ToDateTime(xmnr.RiQi);
            dateriqi.Text = dt.ToString("yyyy-MM-dd HH:mm");
            //分数
            textzongfen.Text = xmnr.ZongFen;
            //护理级别
            combohulijibie.Text = xmnr.HuLiJiBie;
            //责任护理签名
            textzerenhushiqianming.Text = xmnr.ZenRenHuShiQianMing;
            this.simpleButtonbaocun.Enabled = true;
        }

        //计算总分   
        #region
        public void SunTotal()
        {
            int Total = 0;
            int Total1 = 0;
            int Total2 = 0;
            int Total3 = 0;
            int Total4 = 0;
            int Total5 = 0;
            int Total6 = 0;
            int Total7 = 0;
            int Total8 = 0;
            int Total9 = 0;
            //进食
            if (radioGroupjinshi.SelectedIndex == 0)
                Total = 10;
            else if (radioGroupjinshi.SelectedIndex == 1)
                Total = 5;
            else
                Total = 0;
            //洗澡
            if (radioGroupxizao.SelectedIndex == 0)
                Total1 = 5;
            else
                Total1 = 0;
            //修饰
            if (radioGroupxiushi.SelectedIndex == 0)
                Total2 = 5;
            else
                Total2 = 0;
            //穿衣
            if (radioGroupchuanyi.SelectedIndex == 0)
                Total3 = 10;
            else if (radioGroupchuanyi.SelectedIndex == 1)
                Total3 = 5;
            else
                Total3 = 0;
            //控制大便
            if (radioGroupkongzhidabian.SelectedIndex == 0)
                Total4 = 10;
            else if (radioGroupkongzhidabian.SelectedIndex == 1)
                Total4 = 5;
            else
                Total4 = 0;
            //控制小便
            if (radioGroupkongzhixiaobian.SelectedIndex == 0)
                Total5 = 10;
            else if (radioGroupkongzhixiaobian.SelectedIndex == 1)
                Total5 = 5;
            else
                Total5 = 0;
            //如厕
            if (radioGroupruce.SelectedIndex == 0)
                Total6 = 10;
            else if (radioGroupruce.SelectedIndex == 1)
                Total6 = 5;
            else
                Total6 = 0;
            //床椅转移
            if (radioGroupchuangyizhuanyi.SelectedIndex == 0)
                Total7 = 15;
            else if (radioGroupchuangyizhuanyi.SelectedIndex == 1)
                Total7 = 10;
            else if (radioGroupchuangyizhuanyi.SelectedIndex == 2)
                Total7 = 5;
            else if (radioGroupchuangyizhuanyi.SelectedIndex == 3)
                Total7 = 0;
            //平底行走
            if (radioGrouppingdixingzou.SelectedIndex == 0)
                Total8 = 15;
            else if (radioGrouppingdixingzou.SelectedIndex == 1)
                Total8 = 10;
            else if (radioGrouppingdixingzou.SelectedIndex == 2)
                Total8 = 5;
            else if (radioGrouppingdixingzou.SelectedIndex == 3)
                Total8 = 0;
            //上下楼梯
            if (radioGroupshangxialouti.SelectedIndex == 0)
                Total9 = 10;
            else if (radioGroupshangxialouti.SelectedIndex == 1)
                Total9 = 5;
            else
                Total9 = 0;
            Sum = Total + Total1 + Total2 + Total3 + Total4 + Total5 + Total6 + Total7 + Total8 + Total9;
            textzongfen.Text = Sum.ToString();
            //根据项目内容的分值判断对应的能力等级分数
            if (Sum >= 0 && Sum <= 40)
                radioGroupzilinenglidengji.SelectedIndex = 0;
            if (Sum >= 41 && Sum <= 60)
                radioGroupzilinenglidengji.SelectedIndex = 1;
            if (Sum >= 61 && Sum <= 99)
                radioGroupzilinenglidengji.SelectedIndex = 2;
            if (Sum > 99)
                radioGroupzilinenglidengji.SelectedIndex = 3;
        }
        #endregion


        //总分
        private void textzongfen_EditValueChanged(object sender, EventArgs e)
        {
            SunTotal();
        }

        //计算选中栏位分数相加
        private void radioGroupjinshi_SelectedIndexChanged(object sender, EventArgs e)
        {
            SunTotal();
        }

        //取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}